package hlhj.fhp.tvlib.adapter

import android.widget.TextView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.GoLoginDialog
import hlhj.fhp.tvlib.javabean.BaseBean
import hlhj.fhp.tvlib.javabean.CommentListBean
import hlhj.fhp.tvlib.utils.TimeUt

/**
 * Created by Never Fear   on 2018\9\6 0006.
Never More....
 */
class NewLiveCommentAdp(private val datas:ArrayList<CommentListBean.DataBean>)
    :BaseQuickAdapter<CommentListBean.DataBean,BaseViewHolder>(R.layout.newlive_item,datas) {
    override fun convert(helper: BaseViewHolder?, item: CommentListBean.DataBean?) {
        if (item?.head_pic!!.contains("http")){
            Glide.with(mContext).load(item?.head_pic).into(helper?.getView(R.id.itemIcon)!!)

        }else{
            Glide.with(mContext).load(Contents.IMG_URL+item?.head_pic).into(helper?.getView(R.id.itemIcon)!!)

        }
        helper?.setText(R.id.itemName,item?.user_name)
        helper?.setText(R.id.itemContent,item?.content)
        helper?.setText(R.id.itemTime,TimeUt.convertTimeToFormat(item?.create_at!!.toLong()))
        helper?.setText(R.id.btZan,item?.laud_num.toString())
        if (item?.is_laud==0){
            val textDrawable = helper?.getView<TextView>(R.id.btZan)
            val drawableLeft = mContext.getResources().getDrawable(
                    R.drawable.ic_home_praise_normal)
            textDrawable?.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null)
            textDrawable?.setCompoundDrawablePadding(4)
            textDrawable?.setOnClickListener {
                OkGo.post<String>(Contents.BaseUrl+Contents.ItsGood)
                        .params("token",TMSharedPUtil.getTMToken(mContext))
                        .headers("token",TMSharedPUtil.getTMToken(mContext))
                        .params("id",item?.id)
                        .params("type",0)
                        .execute(object :StringCallback(){
                            override fun onSuccess(response: Response<String>?) {
                                val body = response?.body()
                                val baseBean = Gson().fromJson(body, BaseBean::class.java)
                                if (baseBean.code==1){
                                    Toast.makeText(mContext,"点赞成功",Toast.LENGTH_SHORT).show()
                                    datas[helper?.adapterPosition!!-1].is_laud=1
                                    datas[helper?.adapterPosition!!-1].laud_num=datas[helper?.adapterPosition!!-1].laud_num+1
                                    notifyDataSetChanged()
                                }else{
                                    GoLoginDialog(mContext).show()
                                }
                            }
                        })
            }

        }else{
            val textDrawable = helper?.getView<TextView>(R.id.btZan)
            val drawableLeft = mContext.getResources().getDrawable(
                    R.drawable.ic_home_praise_select)
            textDrawable?.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null)
            textDrawable?.setCompoundDrawablePadding(4)
            textDrawable?.setOnClickListener {
                OkGo.post<String>(Contents.BaseUrl+Contents.ItsGood)
                        .params("token",TMSharedPUtil.getTMToken(mContext))
                        .params("id",item?.id!!)
                        .headers("token",TMSharedPUtil.getTMToken(mContext))
                        .params("type",0)
                        .execute(object :StringCallback(){
                            override fun onSuccess(response: Response<String>?) {
                                val body = response?.body()
                                val baseBean = Gson().fromJson(body, BaseBean::class.java)
                                if (baseBean.code==1){
                                    datas[helper?.adapterPosition!!-1].is_laud=0
                                    datas[helper?.adapterPosition!!-1].laud_num=datas[helper?.adapterPosition!!-1].laud_num-1
                                    notifyDataSetChanged()
                                }else{
                                    GoLoginDialog(mContext).show()
                                }
                            }
                        })
            }
        }
    }
}